package com.ycy.ss;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.ycy.ss.sync.Info;
import com.ycy.ss.sync.ReadInfo;


public class SyncJob implements Job{
	private Logger logger = LogManager.getLogger(SyncJob.class);

	public void execute(JobExecutionContext context) throws JobExecutionException{
		logger.info("===== start sync job =====");

		if (SSUtil.JOB_LOCK) {
			logger.warn("job is running...skip this time");
			return;
		} else {
			try {
				SSUtil.JOB_LOCK = true;
				List<Info> allInfo = new ArrayList<Info>();

				// read
				for (String from : Constant.getSyncAllFrom()) {
					try {
						List<Info> fromInfoList = ReadInfo.read(from);
						if (fromInfoList == null || fromInfoList.size() == 0) {
							logger.info("sync job,read info[" + from + "] fromInfoList == null or size == 0");
							continue;
						} else {
							logger.info("sync job,read info[" + from + "] fromInfoList.size == " + fromInfoList.size());
						}
						allInfo.addAll(fromInfoList);
					} catch (Exception e) {
						logger.warn("", e);
					}
				}

				// add to queue
				logger.info("sync job read info:" + allInfo.size());
				WriteJob.QUEUE_LIST.addAll(allInfo);
			} catch (Exception e) {
				logger.warn("", e);
			} finally {
				SSUtil.JOB_LOCK = false;
			}
		}

		logger.info("===== end sync job =====");
	}
}
